using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System.Reflection;
using QQZiCore.Mvc;
using QQZiFramework.Mvc;
using QQZiFramework;
using System.Configuration;
using QQZiFramework.MQ;

namespace QQZiCore.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {

            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region 添加Swagger
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
                //// 获取xml文件名
                //var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                //// 获取xml文件路径
                //var xmlPath = Path.Combine(System.AppContext.BaseDirectory, xmlFile);
                //// 添加控制器层注释，true表示显示控制器注释
                //options.IncludeXmlComments(xmlPath, true);
            });
            #endregion
            //IMvcBuilder builder = services.AddControllers();
            //foreach (Assembly assembly in Loader.SystemLoader.ModuleAssemblies)
            //{
            //    builder.AddApplicationPart(assembly);
            //}
            services.AddResponseCaching();            
            services.AddDBContext();
            services.AddControllers();
            services.UserQQZiFrameworkMQ(Configuration);//要在DBContext.ServiceProvider(BuildServiceProvider)赋值之前调用
            services.BuildServiceProvider4QQZiFramework();
            //IConfigurationSection section = Configuration.GetSection("Modules");

            //            if (section != null && !string.IsNullOrEmpty( section.GetValue<string>("libs") ))
            //            {
            //                string[] libs = section.GetValue<string>("libs").Split(',');
            //                string dllRoot = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug\\net6.0\\", "\\Modules");
            //#if DEBUG
            //                dllRoot += "\\netstandard2.0";
            //#endif
            //                foreach (string libFile in libs)
            //                {
            //                    var ass = AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(dllRoot, libFile));
            //                    builder.AddApplicationPart(ass);
            //                }
            //            }

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            // 启用Swagger
            app.UseSwagger();
            // 启动Swagger UI
            app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = "api";
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "API");
            });
            // app.UseSession();// 必须在 UseMvc 之前调用
            app.UseResponseCaching();
            app.UseQQZI(env, Configuration,typeof(QQZiRequestMiddleware), 1);
            app.UseStaticFiles();
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            //SH3H.Service.BaseExtServer.OnGetDBContext += OnGetDBContext;
        }

        static DBContext OnGetDBContext(string oemName)
        {            
            return OEMMemberShip.GetInstance(oemName).CreateDBContext();
        }
    }
}
